function [] = plot_stdhist(stdmat, data, in)
% =========================================================================

cucd = pwd; 
[e, in] = ch_field(in, 'savepath', cucd); 
modenames = in.modenames; 
varnames = in.varnames; 
savename = in.savename;  
savepath = in.savepath;

% Decide whether there is data passed in
if isempty(data)
    flag_noData = 1;
else
    flag_noData = 0;
end

%Check to make sure dimensions of data match
if ~flag_noData
    if size(data, 2) ~= length(varnames) 
        error('number of series in the data does not match the number of varnames')
    elseif size(data, 2) ~= size(stdmat, 1); 
        error('number of series in the data does not match the number of variables in stdmat')
    end
end
nz = length(varnames); 
if length(modenames) ~= size(stdmat, 3); 
    error('number of modes in stdmat does not match the number of modenames'); 
else 
    nmode = length(modenames); 
end


 %% Kernel Density Plots
 
 % Different styles of lines to use when plotting
lineTypes = {'blue','black','green','--blue','--black','--green'};
 
ghand = figure;

for ii = 1:nz;
    subplot(ceil(nz/2),2,ii);
        
    % Calculate STD of the data
    if ~flag_noData
        try
            temp_base = nanstd(data(:,ii));
        catch
            disp('problems with statistics toolbox: calling NANSTD failed')
            temp_base = std(data(:,ii));
        end
    end
    
    % Overlay kernel densities onto one plot for each model
    for jj = 1:nmode            
            
    [kd xaxis] = ksdensity(stdmat(ii,:,jj));
            
    % Check to see if the line types have been used up
    if jj > length(lineTypes)
        plot(xaxis, kd, 'LineWidth',2);
    else
        plot(xaxis, kd, lineTypes{jj}, 'LineWidth', 2);
    end
    hold on

    end
    title(varnames{ii});
            
    if ~flag_noData
        hand=vline(temp_base, 'r-');
        set(hand,'LineWidth',4);
    end
end

if isfield(in,'position')
    set(ghand, 'paperPosition', in.position);
end

% Create legend and center at bottom
h = legend(modenames, 'Orientation', 'horizontal');
p = get(h,'Position');
p(1)= 0.37;
p(2)= 0;
set(h,'Position',p);
 

if nmode ~= 1;
    suptitle('Z STD Density',14);
else
    suptitle(['Z STD Density', modenames{1}], 14);
end

save_multipages(ghand, [savename,'Density'], savepath);

close all;
end 

% %% Volatility Histograms
% ghand = figure;
% 
% for ii=1:nz;
%     for jj=1:nmode
%         
%         if ~flag_noData
%             try
%                 temp_base=nanstd(data(:,ii));
%             catch
%                 disp('problems with statistics toolbox: calling NANSTD failed')
%                 temp_base=std(data(:,ii));
%             end
%         end
%         if nmode ~=1
%            subplot(nz,nmode,jj + (ii-1)*nmode);
%         else
%             subplot(3,ceil(nz/3),jj + (ii-1)*nmode);
%         end
%         
%         temp = stdmat(ii, :, jj); 
%         hist(temp,20);
%         
%         if nmode ~= 1
%             if ii == 1; title(modenames{jj}); end
%             ylabel(varnames{ii});
%         else
%             title(varnames{ii})
%         end
%         
%         if ~flag_noData
%             hand=vline(temp_base, 'r-');
%             set(hand,'LineWidth',4);
%         end
%     end
% end
% 
% if nmode ~= 1;
%     suptitle('Z STD ',14);
% else
%     suptitle(['Z STD ', modenames{1}], 14);
% end
% 
% subtitulo(savepath);
% set(ghand,'PaperPosition',[0.05 0.03 8.5 11]);
% 
% save_multipages(ghand, savename, savepath);
